﻿using MySql.Data.MySqlClient;
using studentskicentarETFa;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for WindowUpravnik.xaml
    /// </summary>
    public partial class WindowUpravnik : Window
    {
        public WindowUpravnik()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DodavanjeZaposlenika dz = new DodavanjeZaposlenika(PregledZaposlenikaGrid);
            dz.Show();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            BindMyDataZaposlenik();
        }

        private void New_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("");  
                
        }

        private void ObrisiZapButtona_Click(object sender, RoutedEventArgs e)
        {
            
            //try
            //{

                string MyConnection2 = "server=localhost;user=root;pwd=;database=Centar";
                string Query = "delete from zaposlenik where ID_Clana='" + this.ObrisiZaposlenikaTB.Text + "';";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
                MySqlDataReader MyReader2;
                MyConn2.Open();
                MyReader2 = MyCommand2.ExecuteReader();
                MessageBox.Show("Uspjesno ste obrisali zaposlenika.");
                MyConn2.Close();
                BindMyDataZaposlenik();

            
/*
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }*/
           
        }

        private void BindMyDataZaposlenik()
        {
            MySqlConnection con = null;
            string connectionString = "server=localhost;user=root;pwd=;database=centar";
            con = new MySqlConnection(connectionString);
            try
            {
                con.Open();
                MySqlCommand comm = new MySqlCommand("SELECT * FROM Zaposlenik", con);
                comm.CommandText = "SELECT * from Zaposlenik";
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(comm);
                da.Fill(ds);
                PregledZaposlenikaGrid.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {

                MessageBox.Show("Obrisano","Brisanje");
            }
            finally
            {
                con.Close();
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            MainWindow m = new MainWindow();
            m.Show();
            this.Hide();
        }

        private void Grid_Loaded_1(object sender, RoutedEventArgs e)
        {
            webBrowser1.Navigate("https://mail.google.com");
        }

        private void PregledZaposlenikaGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            studentskicentarETFa.DAL d = studentskicentarETFa.DAL.Instanca;
            d.kreirajKonekciju("localhost", "Centar", "root", "");
            studentskicentarETFa.DAL.ZaposlenikDAO z = d.getDAO.getZaposlenikDAO();
            List<Zaposlenik> p = new List<Zaposlenik>();
            p = z.getAll();
            ObrisiZaposlenikaTB.Text = p[PregledZaposlenikaGrid.SelectedIndex].ID_Clana1;
        }

      

      
    }
}
